package cn.edu.fzu.homemaking.controller;

import cn.edu.fzu.homemaking.api.CompanyApi;
import cn.edu.fzu.homemaking.domain.entity.CompanyServer;
import cn.edu.fzu.homemaking.domain.enums.ServerPriceModeEnum;
import cn.edu.fzu.homemaking.domain.enums.ServerStatusEnum;
import cn.edu.fzu.homemaking.util.ResultUtil;
import cn.edu.fzu.homemaking.web.Result;
import io.swagger.annotations.*;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author jiebin
 * @Date 2023/4/25
 */
@RestController
@RequestMapping("/api/user/company")
@Api(tags = "公司相关接口")
public class CompanyController {

    @Resource
    private CompanyApi companyApi;

    @GetMapping("/listServer")
    @ApiOperation(value = "查询服务列表", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "companyId", value = "公司id"),
            @ApiImplicitParam(name = "labelId", value = "服务标签"),
            @ApiImplicitParam(name = "name", value = "服务名称"),
            @ApiImplicitParam(name = "chargeMode", value = "计费模式 0=按次数计费，1=按时长计费")}
    )
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result listServer(Long companyId, Long labelId, String name, ServerPriceModeEnum chargeMode) {
        int maxSize = 100;
        int pageNum = 1;
        List<CompanyServer> serverList = new LinkedList<>();
        do {
            Result result = companyApi.listServer(companyId, labelId, name, chargeMode, ServerStatusEnum.ON, pageNum++, maxSize);
            List<CompanyServer> list = ResultUtil.parsePageValue(result, CompanyServer.class);
            if(CollectionUtils.isEmpty(list)){
                break;
            }
            serverList.addAll(list);
        }while (serverList.size() == maxSize);
        return new Result(serverList);
    }

    @GetMapping("getServerById")
    @ApiOperation(value = "根据id获取服务",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "服务id",required = true)
    })
    public Result getServerById(Long id){
        return companyApi.getById(id);
    }
}
